<?php

require_once('/home/itrial/www/appconf.php');
require_once "CMS.class.php";

$local_config = array(
    'this_cgi'       => 'crontab/points_management.php',
	'table'			=>	'points_management',
);

class backend_crontab_points_management extends CMS{


	function setup(){
		$this->run_modes('manage');
		$this->start_mode('manage');
		$this->set_path_info_position(array('id'=>1));
		parent::setup();
	}


	#no no login
	
	function cgiapp_prerun(){
	}
	
	
	function action_manage(){
	
		$dbh_r = $this->db_connect('read');
		$dbh_w = $this->db_connect('write');
		
		
		
		$a_ids = db_selectcol_array($dbh_r, 'select id from '.$this->param('table').' where is_processing=1 and status=1');
		
		
		if(count($a_ids) > 0){
			AppendFile('/home/itrial/logs/points_management.log', "Processing: ".implode(",", $a_ids).". Skip this time.\n");
			return "Finish";
		}
		
		$a_ids = db_selectcol_array($dbh_r, 'select id from '.$this->param('table').' where is_processed=0 and status=1 and (NOW()- INTERVAL 5 MINUTE > lastmodify)');
		
		//For testing
		//$a_ids = db_selectcol_array($dbh_r, 'select id from '.$this->param('table').' where is_processed=0 and status=1 and id=8');
		

		$points = getModel('CMS_Points');
		foreach ($a_ids as $id){
			db_do($dbh_w, "update ".$this->param('table')." set is_processing=1 where id=?", $id);
			$this->process($dbh_w, $id, $points);
			db_do($dbh_w, "update ".$this->param('table')." set is_processing=0, is_processed=1 where id=?", $id);
			AppendFile('/home/itrial/logs/points_management.log', "Processing: $id \n");
		}
		
		echo "Done\n";
		return "OK";
  }
  
	function process($dbh_w, $id, $points){
	
		$h_row = db_selectrow_hash($dbh_w, 'select * from '.$this->param('table').' where id=?', $id);
		
		$operation_type = $h_row['operation'];
		$points_action = $h_row['points_action'];
		
		
		$a_members = $this->get_member_list($dbh_w, $h_row);
		

		$success_processed_id = array();
		$failed_processed_id = array();
		foreach ($a_members as $member){
		
			$action_name = "";
			if($h_row['operation'] == 1){
				$action_name = "system_add";
			}else if($h_row['operation'] == 2){
				$action_name = "system_deduction";
			}
			
			$reason = $h_row['cname'];
			if($h_row['is_default_reason'] == 1){
				$result = array();
				$action_name = db_select_one($dbh_w, "select action from points_action_type where id=?", $h_row['points_action']);
				$result = $points->points_record($member, $action_name, $reason);
				echo "Default action";
				//print_r($result);
				if($result['ret'] == 1){
					array_push($success_processed_id, $member);
				}else{
					array_push($failed_processed_id, $member);
				}
			}else{
				$result = array();
				$result = $points->points_record($member, $action_name, $reason, 0, $h_row['points']);
				echo "Non default action";
				//print_r($result);
				if($result['ret'] == 1){
					array_push($success_processed_id, $member);
				}else{
					array_push($failed_processed_id, $member);
				}
			}
			echo "Processing ID: $member\n";

		}
			AppendFile('/home/itrial/logs/points_management.log', "Processed member:\n");
			AppendFile('/home/itrial/logs/points_management.log', "Success:".implode(",",$a_members)."\n");
			AppendFile('/home/itrial/logs/points_management.log', "Success:".implode(",",$success_processed_id)."\n");
			AppendFile('/home/itrial/logs/points_management.log', "Failed:".implode(",",$failed_processed_id)."\n");
		return;
		//return array($success_processed_id, $failed_processed_id);
	}
	
	function get_member_list($dbh_w, $h_row){
		
		$a_members = array();
		if($h_row['is_all_member'] == 1){
			//$a_members = db_selectcol_array($dbh_w, 'select * from member_imore where status=1');
			$a_members = db_selectcol_array($dbh_w, 'select * from member_imore where id>20000 and status=1');
		}else{
			$count = db_select_one($dbh_w, 'select count(*) from member_imore where id=?', $h_row['member']);
			if($count >0){
				array_push($a_members, $h_row['member']);
			}
		}
		return $a_members;
	}
	



    


}//end class

$webapp = new backend_crontab_points_management(
   array('PARAMS' => array_merge($site_global_config, $local_config))
         );
$webapp->run();

/* Custom Helper Function Implementation start here */

?>

